package pers.xiaojun.boot.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.web.core.util.ServletUtils;

import java.io.IOException;

import static pers.xiaojun.boot.common.constants.GlobalBusinessCodeConstants.UNAUTHORIZED;

/**
 * 未登录处理器
 *
 * @author xiaojun
 * @since 2025-10-04
 */
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.debug("[commence][访问 URL({}) 时，没有登录]", request.getRequestURI(), authException);
        // 返回 401
        ServletUtils.writeJSON(response, CommonResult.error(UNAUTHORIZED));
    }

}
